<?php

namespace App\AgentAdmin\Controllers;

use Dcat\Admin\Admin;
use App\Admin\Repositories\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\AgentAdmin\Metrics\Orders;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class OrderController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('订单')
            ->description('订单列表')
            ->body(function ($row) {
                // $row->column(4, new Orders\DayOrderNumCount());
                // $row->column(4, new Orders\DayOrderMoneyCount());
            })
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->whereNotNull('paid_at')->where(['agent_id'=>(Admin::user()->id),'is_deduct'=>0])->orderBy('created_at','desc');//->whereNotNull('paid_at')
            $grid->column('video_id');
            $grid->column('no')->copyable();
            $grid->column('agent_price','金额')->sortable();
            $grid->column('paid_at')->sortable()->display(function($paid_at){
                if(!boolval($paid_at)){
                    return "<span style='color:gray'>未支付</span>";
                }
                return "<span style='color:green'>$paid_at</span>";
            });
            $grid->column('valid')->sortable()->using([1 => '支付完成', 0 => '未支付'])->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                0 => 'danger',
                1 => 'success',
            ]);
            $grid->column('created_at')->sortable();
        
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRowSelector();
            $grid->disableBatchActions();
            $grid->disableDeleteButton();
            // $grid->disableRefreshButton();
            // 禁用详情按钮
            $grid->disableViewButton();

            $grid->disableActions();
            // $grid->quickSearch(['no', 'video_id']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
           
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
